/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Public License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/opensource/licenses.mspx#Ms-PL

 ****************************************************************************************/
using System;

namespace Microsoft.CSA.FeedSync.FeedSyncLibrary
{
    public class Sharing
    {
        private System.Xml.XmlElement m_XmlElement;

        private string m_Since;
        private string m_Until;
        private string m_NamespacePrefix;

        private System.DateTime? m_ExpiresDateTime = null;

        private System.Collections.Generic.List<Microsoft.CSA.FeedSync.FeedSyncLibrary.Related> m_RelatedList = new System.Collections.Generic.List<Microsoft.CSA.FeedSync.FeedSyncLibrary.Related>();

        public Sharing(System.Xml.XmlElement i_SharingXmlElement, System.Xml.XmlNamespaceManager i_XmlNamespaceManager)
        {
            if (i_SharingXmlElement == null)
                throw new System.ArgumentException("i_SharingXmlElement must be non-null!");

            if (i_XmlNamespaceManager == null)
                throw new System.ArgumentException("i_XmlNamespaceManager must be non-null!");

            bool InvalidXmlElement =
                (i_SharingXmlElement.LocalName != Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.SHARING_ELEMENT_NAME) ||
                (i_SharingXmlElement.NamespaceURI != Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.FEEDSYNC_XML_NAMESPACE_URI);

            if (InvalidXmlElement)
                throw new System.ArgumentException("Invalid i_SharingXmlElement!");

            m_XmlElement = i_SharingXmlElement;

            if (m_XmlElement.HasAttribute(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.SINCE_ATTRIBUTE))
                m_Since = m_XmlElement.GetAttribute(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.SINCE_ATTRIBUTE);

            if (m_XmlElement.HasAttribute(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.UNTIL_ATTRIBUTE))
                m_Until = m_XmlElement.GetAttribute(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.SINCE_ATTRIBUTE);

            if (m_XmlElement.HasAttribute(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.EXPIRES_ATTRIBUTE))
            {
                string Expires = m_XmlElement.GetAttribute(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.EXPIRES_ATTRIBUTE);
                m_ExpiresDateTime  = System.DateTime.Parse(Expires);
            }

            m_NamespacePrefix = Microsoft.CSA.FeedSync.FeedSyncLibrary.XmlNamespaceManagerHelper.InitializeXmlNamespaceManager
                (
                i_SharingXmlElement.OwnerDocument, 
                i_XmlNamespaceManager,
                Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.FEEDSYNC_XML_NAMESPACE_PREFIX,
                Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.FEEDSYNC_XML_NAMESPACE_URI
                );

            string XPathQuery = System.String.Format
                (
                "{0}:{1}",
                m_NamespacePrefix,
                Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.RELATED_ELEMENT_NAME
                );

            System.Xml.XmlNodeList RelatedNodeList = i_SharingXmlElement.SelectNodes
                (
                XPathQuery,
                i_XmlNamespaceManager
                );

            foreach (System.Xml.XmlElement RelatedXmlElement in RelatedNodeList)
            {
                Microsoft.CSA.FeedSync.FeedSyncLibrary.Related Related = new Microsoft.CSA.FeedSync.FeedSyncLibrary.Related
                    (
                    this,
                    RelatedXmlElement
                    );

                m_RelatedList.Add(Related);
            }
        }

        public Sharing(System.Xml.XmlDocument i_XmlDocument, System.Xml.XmlNamespaceManager i_XmlNamespaceManager, string i_Since, string i_Until, System.DateTime? i_ExpiresDateTime)
        {
            m_NamespacePrefix = Microsoft.CSA.FeedSync.FeedSyncLibrary.XmlNamespaceManagerHelper.InitializeXmlNamespaceManager
                (
                i_XmlDocument,
                i_XmlNamespaceManager,
                Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.FEEDSYNC_XML_NAMESPACE_PREFIX,
                Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.FEEDSYNC_XML_NAMESPACE_URI
                );

            string ElementName = System.String.Format
                (
                "{0}:{1}",
                m_NamespacePrefix,
                Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.SHARING_ELEMENT_NAME
                );

            m_XmlElement = i_XmlDocument.CreateElement
                (
                ElementName,
                Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.FEEDSYNC_XML_NAMESPACE_URI
                );

            m_Since = i_Since;
            m_Until = i_Until;
            if (i_ExpiresDateTime.HasValue)
                m_ExpiresDateTime = ((System.DateTime)i_ExpiresDateTime).ToUniversalTime();

            if (!System.String.IsNullOrEmpty(m_Since))
                m_XmlElement.SetAttribute(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.SINCE_ATTRIBUTE, m_Since);

            if (!System.String.IsNullOrEmpty(m_Until))
                m_XmlElement.SetAttribute(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.UNTIL_ATTRIBUTE, m_Until);

            if (m_ExpiresDateTime != null)
                m_XmlElement.SetAttribute(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.EXPIRES_ATTRIBUTE, ((System.DateTime)m_ExpiresDateTime).ToString(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.DATE_STRING_FORMAT));
        }

        public System.Xml.XmlElement XmlElement
        {
            get
            {
                return m_XmlElement;
            }
        }

        public string Since
        {
            get
            {
                return m_Since;
            }
            set
            {
                if (System.String.IsNullOrEmpty(value))
                    throw new System.ArgumentException("Invalid value!");

                m_Since = value;
                m_XmlElement.SetAttribute(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.SINCE_ATTRIBUTE, value);
            }
        }

        public string Until
        {
            get
            {
                return m_Until;
            }
            set
            {
                if (System.String.IsNullOrEmpty(value))
                    throw new System.ArgumentException("Invalid value!");

                m_Until = value;
                m_XmlElement.SetAttribute(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.UNTIL_ATTRIBUTE, value);
            }
        }

        public System.DateTime? ExpiresDateTime
        {
            get
            {
                return m_ExpiresDateTime;
            }
        }

        public string NamespacePrefix
        {
            get
            {
                return m_NamespacePrefix;
            }
        }

        public void AddRelated(Microsoft.CSA.FeedSync.FeedSyncLibrary.Related i_Related)
        {
            if (m_RelatedList.Contains(i_Related))
                throw new System.ArgumentException("i_Related already added!");

            if (i_Related.XmlElement.OwnerDocument != m_XmlElement.OwnerDocument)
            {
                System.Xml.XmlElement ImportedRelatedXmlElement = (System.Xml.XmlElement)m_XmlElement.OwnerDocument.ImportNode
                    (
                    i_Related.XmlElement,
                    true
                    );

                i_Related = new Microsoft.CSA.FeedSync.FeedSyncLibrary.Related
                    (
                    this,
                    ImportedRelatedXmlElement
                    );
            }

            m_XmlElement.AppendChild(i_Related.XmlElement);

            m_RelatedList.Add(i_Related);
        }
    }
}
